1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import java.util.*;
36 import javax.management.*;
37
38 public class SetWrongTypeAttributeTest {
39
40
41
42 private static final Object[][] TEST_VALUES = {
43 {"Foo", null, 5, "false", Collections.<String,String>emptyMap()},
44 {"Name", 5, false, Collections.<String,String>emptyMap()},
45 {"Properties", 5, false, Collections.singleton("foo")},
46 };
47
48 public static interface BlahMBean {
49 public boolean isFoo();
50 public void setFoo(boolean foo);
51
52 public String getName();
53 public void setName(String name);
54
55 public Map<String,String> getProperties();
56 public void setProperties(Map<String,String> map);
57 }
58
59 public static interface BlahMXBean {
60 public boolean isFoo();
61 public void setFoo(boolean foo);
62
63 public String getName();
64 public void setName(String name);
65
66 public Map<String,String> getProperties();
67 public void setProperties(Map<String,String> map);
68 }
69
70 public static class BlahBase {
71 public boolean isFoo() {
72 return foo;
73 }
74 public void setFoo(boolean foo) {
75 this.foo = foo;
76 }
77
78 public String getName() {
79 return name;
80 }
81 public void setName(String name) {
82 this.name = name;
83 }
84
85 public Map<String,String> getProperties() {
86 return properties;
87 }
88 public void setProperties(Map<String,String> map) {
89 this.properties = map;
90 }
91
92 private boolean foo;
93 private String name;
94 private Map<String,String> properties;
95 }
96
97 public static class Blah extends BlahBase implements BlahMBean {}
98
99 public static class MXBlah extends BlahBase implements BlahMXBean {}
100
101 public static class StdBlah extends StandardMBean implements BlahMBean {
102 public StdBlah() throws NotCompliantMBeanException {
103 super(BlahMBean.class);
104 }
105
106 public boolean isFoo() {
107 return foo;
108 }
109 public void setFoo(boolean foo) {
110 this.foo = foo;
111 }
112
113 public String getName() {
114 return name;
115 }
116 public void setName(String name) {
117 this.name = name;
118 }
119
120 public Map<String,String> getProperties() {
121 return properties;
122 }
123 public void setProperties(Map<String,String> map) {
124 this.properties = map;
125 }
126
127 private boolean foo;
128 private String name;
129 private Map<String,String> properties;
130 }
131
132 public static class StdMXBlah extends StandardMBean implements BlahMXBean {
133 public StdMXBlah() throws NotCompliantMBeanException {
134 super(BlahMXBean.class, true);
135 }
136
137 public boolean isFoo() {
138 return foo;
139 }
140 public void setFoo(boolean foo) {
141 this.foo = foo;
142 }
143
144 public String getName() {
145 return name;
146 }
147 public void setName(String name) {
148 this.name = name;
149 }
150
151 public Map<String,String> getProperties() {
152 return properties;
153 }
154 public void setProperties(Map<String,String> map) {
155 this.properties = map;
156 }
157
158 private boolean foo;
159 private String name;
160 private Map<String,String> properties;
161 }
162
163 public static void main(String[] args) throws Exception {
164 test("Standard Blah", new Blah());
165 test("StandardMBean implementing Blah", new StdBlah());
166 test("StandardMBean wrapping Blah",
167 new StandardMBean(new Blah(), BlahMBean.class));
168 test("MXBean Blah", new MXBlah());
169 test("StandardMBean implementing MXBean Blah", new StdMXBlah());
170 test("StandardMBean wrapping MXBean Blah",
171 new StandardMBean(new MXBlah(), BlahMXBean.class, true));
172
173 if (failure == null)
174 System.out.println("TEST PASSED");
175 else
176 throw new Exception("TEST FAILED: " + failure);
177 }
178
179 private static void test(String what, Object obj) throws Exception {
180 System.out.println(what + "...");
181 MBeanServer mbs = MBeanServerFactory.newMBeanServer();
182 ObjectName on = new ObjectName("a:b=c");
183 mbs.registerMBean(obj, on);
184 for (Object[] testValue : TEST_VALUES) {
185 String attrName = (String) testValue[0];
186 for (int i = 1; i < testValue.length; i++) {
187 Object value = testValue[i];
188 final String doing =
189 "setAttribute(" + attrName + ", " + value + ")";
190 try {
191 mbs.setAttribute(on, new Attribute("Foo", 5));
192 fail(what, doing + " succeeded but should fail!");
193 } catch (InvalidAttributeValueException e) {
194 final String msg =
195 doing + ": OK, got expected " +
196 "InvalidAttributeValueException";
197 System.out.println(msg);
198 } catch (Throwable e) {
199 fail(what, doing + " got wrong exception: " + e);
200 e.printStackTrace(System.out);
201 }
202 }
203 }
204 }
205
206 private static void fail(String what, String msg) {
207 failure = what + ": " + msg;
208 System.out.println("FAILED: " + failure);
209 }
210
211 private static String failure;
212 }